package com.example.teatea.controller;

import com.example.teatea.dao.UpdatePsw;
import com.example.teatea.project.Staff;
import com.example.teatea.tools.MD5;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.stage.Stage;

import javax.swing.*;

import static com.example.teatea.controller.LoginController.s;

public class ChangePswController {
    public PasswordField psw;
    public PasswordField pswAgain;
    public Button sureBt;
    public Button closeBt;

    public void toSure(ActionEvent actionEvent) {
        if(psw.getText().equals("")){
            JOptionPane.showMessageDialog(null, "密码不能为空", "提示", JOptionPane.ERROR_MESSAGE);
        }else if(pswAgain.getText().equals("")){
            JOptionPane.showMessageDialog(null, "确认密码不能为空", "提示", JOptionPane.ERROR_MESSAGE);
        }else if(!psw.getText().equals(pswAgain.getText())){
            JOptionPane.showMessageDialog(null, "两次密码不一致", "提示",JOptionPane.ERROR_MESSAGE);
        }else{
            MD5 md5 = new MD5();
            boolean flag = new UpdatePsw().updatePsw(s.getsPhone(), md5.generateMD5(psw.getText()));
            if(flag){
                s.setsPsw(psw.getText());
                JOptionPane.showMessageDialog(null, "修改成功", "提示",JOptionPane.PLAIN_MESSAGE);
                Stage stage = (Stage) sureBt.getScene().getWindow();
                stage.close();
            }else{
                JOptionPane.showMessageDialog(null, "修改失败", "提示",JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public void toClose(ActionEvent actionEvent) {
        Stage stage = (Stage) closeBt.getScene().getWindow();
        stage.close();
    }
}
